package cn.jbit.serialpojo;

import java.io.*;
import java.text.MessageFormat;

/**
 * 源码学院只为培养优秀的bat程序员而生
 *
 * @author 学员张在成
 * @data 2020/6/25
 * 序列化和反序列化person
 */
public class SerialAndUnSerialPerson {

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        //序列化Person
        //serialPerson();
        //反序列化Person
        Person person=unSerialPerson();
        System.out.println(MessageFormat.format("id={0},name={1},pwd={2}",person.getId(),person.getName(), person.getPwd()));
    }
    /**
     * 序列化Person对象
     */
    public  static  void  serialPerson() throws IOException {

        Person person=new Person(1,"张杰","123.com");
        //创建对象输出流对象
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("D:/Person.txt")));
        oos.writeObject(person);
        System.out.println("序列化成功");
        oos.close();
    }

    /**
     * 反序列化Person对象
     */
    public  static  Person  unSerialPerson() throws IOException, ClassNotFoundException {

        ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File("D:/Person.txt")));

        Person person= (Person) ois.readObject();
        System.out.println("反序列化成功");
        return  person;
    }





}
